/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxCmsSource;
import com.ykht.proj.coupon.service.FxCmsSourceService;
import com.ykht.proj.coupon.vo.FxCmsSourceVO;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * cms 素材表
 *
 * @author proj code generator
 * @date 2021-02-01 16:22:52
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxcmssource")
@Api(value = "fxcmssource", tags = "cms 素材表管理")
public class FxCmsSourceController {

	private final FxCmsSourceService fxCmsSourceService;

	/**
	 * 分页查询
	 *
	 * @param page          分页对象
	 * @param fxCmsSourceVO cms 素材表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	//@PreAuthorize("@pms.hasPermission('coupon_fxcmssource_view')" )
	public R getFxCmsSourcePage(Page page, FxCmsSourceVO fxCmsSourceVO) {
		IPage<FxCmsSource> iPage = fxCmsSourceService.queryByPage(page, fxCmsSourceVO);
		return R.ok(iPage);
	}


	/**
	 * 通过id查询cms 素材表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxcmssource_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxCmsSourceService.getById(id));
	}

	/**
	 * 新增cms 素材表
	 *
	 * @param fxCmsSource cms 素材表
	 * @return R
	 */
	@ApiOperation(value = "新增cms 素材表", notes = "新增cms 素材表")
	@SysLog("新增cms 素材表")
	@PostMapping
	// @PreAuthorize("@pms.hasPermission('coupon_fxcmssource_add')" )
	public R save(@RequestBody FxCmsSource fxCmsSource) {
		fxCmsSource.setCreateTime(LocalDateTime.now());
		fxCmsSource.setCreateUser(SecurityUtils.getUserId());
		return R.ok(fxCmsSourceService.save(fxCmsSource));
	}

	/**
	 * 修改cms 素材表
	 *
	 * @param fxCmsSource cms 素材表
	 * @return R
	 */
	@ApiOperation(value = "修改cms 素材表", notes = "修改cms 素材表")
	@SysLog("修改cms 素材表")
	@PutMapping
	// @PreAuthorize("@pms.hasPermission('coupon_fxcmssource_edit')" )
	public R updateById(@RequestBody FxCmsSource fxCmsSource) {
		fxCmsSource.setUpdateTime(LocalDateTime.now());
		fxCmsSource.setUpdateUser(SecurityUtils.getUserId());
		return R.ok(fxCmsSourceService.updateById(fxCmsSource));
	}

	/**
	 * 通过id删除cms 素材表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除cms 素材表", notes = "通过id删除cms 素材表")
	@SysLog("通过id删除cms 素材表")
	@DeleteMapping("/{id}")
	// @PreAuthorize("@pms.hasPermission('coupon_fxcmssource_del')" )
	public R removeById(@PathVariable Long id) {
		return R.ok(fxCmsSourceService.removeById(id));
	}

	/**
	 * 通过id批量删除cms 素材表
	 *
	 * @param ids ids
	 * @return R
	 */
	@ApiOperation(value = "通过id批量删除cms 素材表", notes = "通过id删除cms 素材表")
	@SysLog("通过id删除cms 素材表")
	@DeleteMapping("/del")
	// @PreAuthorize("@pms.hasPermission('coupon_fxcmssource_del')" )
	public R removeById(String ids) {
		String[] arr = ids.split(",");
		for (String s : arr) {
			fxCmsSourceService.removeById(Long.parseLong(s));
		}
		return R.ok();
	}

}
